

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class loginService
 */
@WebServlet("/Activation")
public class Activation extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String userName;
    private String activationKey;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Activation() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		userName = request.getParameter("username");
		System.out.println("username: " + userName);
		activationKey = request.getParameter("activationkey");
		guestUser guestUserObj = new guestUser();
		List result;
		List result2;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        
	        String sqlString = "select * from guestusers where account = '" + userName + "' and ActivationKey = '" + activationKey + "' and isMailValidated = 0";
			Query queryResult = session.createSQLQuery(sqlString).addEntity(guestUser.class);
	        result = queryResult.list();
	        System.out.println("username1: " + userName);
	        System.out.println("activationkey: " + activationKey);
	        if(result.isEmpty()){
			    request.getRequestDispatcher("Error_Activation.html").forward(request, response);
			    System.out.println("username2: " + userName);
        	}
	        else
	        {
	        	
	    		guestUserObj = (guestUser) result.get(0);
	    		//System.out.println("username3: " + userName);
	    		guestUserObj.setIsMailValidated(1);
	    		session.saveOrUpdate(guestUserObj);
	    		System.out.println("username3: " + userName);
	    		session.getTransaction().commit();
		        //HibernateUtil.getSessionFactory().close();
		        request.getRequestDispatcher("Success_Activation.html").forward(request, response);
	        }
			} catch(Exception ex){
				System.out.println("username4: " + userName);
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
}
